package com.sjyl.kaoshitong.adapter;

import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.sjyl.kaoshitong.R;
import com.sjyl.kaoshitong.beans.DocumentItem;
import com.sjyl.kaoshitong.fragment.MyDocumentFileListFragment;

import java.util.List;


/**
 * Created by ChangerLow on 15/6/12.
 * Email: changerlow@qq.com
 */
public class MyDocumentFileRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private static final String TAG = "MyDocumentFileRecyclerViewAdapter";
    private List<DocumentItem> dataSet = null;
    private MyDocumentFileViewHolder.MyOnItemClickListener mItemClickListener;
    public MyDocumentFileRecyclerViewAdapter(List<DocumentItem> list){
        this.dataSet = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_document_file_layout, parent, false);
        return new MyDocumentFileViewHolder(view, mItemClickListener);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        MyDocumentFileViewHolder holder = (MyDocumentFileViewHolder)viewHolder;
        DocumentItem item = dataSet.get(position);
        holder.titleTV.setText(item.getData_name().replaceAll(".pdf",""));
        holder.subtitleTV.setText(item.getData_points()+"金币");
        holder.idTV.setText("id");
        holder.ratingBar.setNumStars(5);
        holder.ratingBar.setRating(item.getPraise_num());
        if (dataSet.get(position).getDown_status().equals("1")){
            holder.downStatusTV.setText("已下载");
        }else{
            holder.downStatusTV.setText("");
        }
        holder.moreBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mItemClickListener != null){
                    mItemClickListener.onItemMoreClick(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public void setOnItemClickListener(MyDocumentFileListFragment listener){
        this.mItemClickListener = listener;
    }

    public static class MyDocumentFileViewHolder extends RecyclerView.ViewHolder {
        public TextView titleTV;
        public TextView subtitleTV;
        public RatingBar ratingBar;
        public TextView idTV;
        public TextView downStatusTV;
        public ImageButton moreBtn;

        public MyDocumentFileViewHolder(final View itemView, final MyOnItemClickListener listener) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null)
                        listener.OnItemClick(v, getAdapterPosition());
                    //AlertDialog dialog =new AlertDialog.Builder(itemView.getContext()).setTitle("This is a Dialog").setMessage("You click "+ getAdapterPosition()+" element!").show();
                    Logger.d(TAG, "Element " + getAdapterPosition() + " clicked.");
                }
            });
            titleTV = (TextView)itemView.findViewById(R.id.list_item_title);
            subtitleTV = (TextView)itemView.findViewById(R.id.list_item_subtitle);
            idTV = (TextView)itemView.findViewById(R.id.list_item_id_label);
            ratingBar = (RatingBar)itemView.findViewById(R.id.ratingbar);
            ratingBar.setMax(5);
            LayerDrawable stars = (LayerDrawable)ratingBar.getProgressDrawable();
            stars.getDrawable(2).setColorFilter(itemView.getResources().getColor(R.color.color_accent), PorterDuff.Mode.SRC_ATOP);
           // stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
            downStatusTV = (TextView)itemView.findViewById(R.id.down_status);
            moreBtn = (ImageButton)itemView.findViewById(R.id.item_more_btn);
        }

        public interface MyOnItemClickListener{
            void OnItemClick(View view, int position);
            void onItemMoreClick(View view, int position);
        }
    }
}
